<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-select v-model="query.key" placeholder="查询条件" class="handle-select mr10" @change="changeKey">
                    <el-option key="1" label="姓名" value="name"></el-option>
                    <el-option key="2" label="状态" value="status"></el-option>
                </el-select>
                <el-input v-model="query.value" placeholder="内容" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="userId" label="ID" width="130" align="center"></el-table-column>
                <el-table-column prop="account" label="用户名"></el-table-column>
                <el-table-column prop="name" label="姓名"> </el-table-column>
                <el-table-column prop="email" label="邮箱"></el-table-column>
                <el-table-column prop="lastLogin" sortable label="最后登录" width="160"> </el-table-column>
                <el-table-column prop="status" label="状态" width="120">
                    <template #default="scope">
                        <el-switch active-color="#ff4949"
                                   v-model="scope.row.status"
                                   :active-value="1"
                                   :inactive-value="0" @change="prohibitUser(scope.row.userId, scope.row.status)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >角色</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="详情" v-model="editVisible" width="30%">
            <h3>已拥有角色</h3>
            <el-table
                :data="existRoles"
                border
                class="table2"
                ref="multipleTable"
                header-cell-class-name="table-header"
        >
            <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
            <el-table-column prop="roleName" label="角色名"></el-table-column>
            <el-table-column prop="description" label="基本功能"></el-table-column>
            <el-table-column label="操作" width="180" align="center">
                <template #default="scope">
                    <el-button
                            v-if="scope.row.status==0"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleDelete(scope.$index, scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
                <h3>未拥有角色</h3>
            <el-table
                    :data="notExistRoles"
                    border
                    class="table2"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="roleName" label="角色名"></el-table-column>
                <el-table-column prop="description" label="基本功能"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                v-if="scope.row.status==0"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleAdd(scope.$index, scope.row)"
                        >添加</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
    import api from "../../../api";
    import user from '@/api/admin/admin'
    export default {
        name: "user",
        data() {
            return {
                query: {
                    userQuery: {
                        name: '',
                        roleName: '',
                        status: null,
                        usertype: null,
                    },
                    pageIndex: 1,
                    pageSize: 8,
                    key:"",
                    value:""
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                existRoles: [],
                notExistRoles:[],
                idx: -1,
                userId: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            changeKey(val){
                this.query.key=val;
            },
            prohibitUser(id, status) {
                status = !status;
                let s = 0
                if (status) {
                    s = 1
                } else {
                    s = 0
                }
                user.prohibitUser(id, s).then(response => {
                    let msg = ""
                    if (status) {
                        msg = "激活成功"
                    } else {
                        msg = "封禁成功"
                    }
                    this.$message({
                        type: 'success',
                        message: msg,
                    })
                    return response;
                }).catch(response => {
                    console.log(response.message)
                    this.$message({
                        type: 'error',
                        message: "操作失败",
                    })
                })
            },
            getData() {
                api.getUserInfo(this.query.pageIndex, this.query.pageSize, this.query.userQuery).then(res => {
                    console.log(res);
                    this.tableData = res.data.item.records;
                    this.pageTotal = res.data.item.total;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.query.userQuery[this.query.key]=this.query.value;
                this.query.pageIndex=1;
                this.getData();
                this.query.userQuery[this.query.key]=null;
            },
            // 添加操作
            handleAdd(index,row) {
                let array = [];
                array[0]={"roleId":row.id};
                api.addRoleToUser(this.userId,array).then(resp=>{
                    if(resp.success){
                        this.$message.success("添加成功");
                        this.editVisible = false;
                    }else {
                        this.$message.error("添加失败");
                    }

                });
            },
            // 删除操作
            handleDelete(index,row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                    .then(() => {
                        let array = [];
                        array[0]={"roleId":row.id};
                        api.deleteRoleToUser(this.userId,array).then(resp=>{
                            if(resp.success){
                                this.$message.success("删除成功");
                                this.editVisible = false;
                            }else {
                                this.$message.error("删除失败");
                            }
                        });
                    })
                    .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = "";
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + " ";
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                api.findRoleByUserId(row.userId).then(resp=>{
                    console.log(resp);
                    this.existRoles = resp.data.item.existRoles;
                    this.notExistRoles = resp.data.item.notExistRoles;
                });
                this.userId=row.userId;
                this.idx = index;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.$set(this.tableData, this.idx, this.form);
            },
            // 分页导航
            handlePageChange(val) {
                this.query.pageIndex=val;
                this.getData(this.query.pageIndex,this.query.pageSize,this.query.userQuery);
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
